//
//  ViewController.m
//  省市区-tableView
//
//  Created by mafeng on 16/7/26.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#import "Province.h"
#import "City.h"
#import "Zone.h"
#import "Province.h"
#import "CityTableViewController.h"
#define kReuse @"UITableViewCell"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *allProArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"省份";
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kReuse];
    // 导入数据
    NSString *path = @"/Users/dllo/DLS160710/UI/HomeWork/省市区-tableView/省市区-tableView/area.txt";
    NSString *areaStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *allArray = [areaStr componentsSeparatedByString:@"\n"];
    self.allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            NSArray *array = [area componentsSeparatedByString:@" "];
            Province *pro = [Province  cityWithName:array[0] number:array[1] array:[NSMutableArray array]];
            [_allProArray addObject:pro];
        } else if(![area hasPrefix:@"    "]) {
            NSString *str = [area substringFromIndex:2];
            Province *pro = [_allProArray lastObject];
             NSArray *array = [str componentsSeparatedByString:@" "];
            City *city = [City cityWithName:array[0] number:array[1] array:[NSMutableArray array]];
            [pro.zoneArray addObject:city];
        } else {
            NSString *str = [area substringFromIndex:4];
            City *city = [[[_allProArray lastObject] zoneArray] lastObject];
             NSArray *array = [str componentsSeparatedByString:@" "];
            Zone *zone = [Zone zoneWithName:array[0] number:array[1]];
            [city.zoneArray addObject:zone];
        }
    }
    NSLog(@"%@", _allProArray);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuse];
    NSArray *array = [_allProArray[indexPath.section] zoneArray];
    cell.textLabel.text = [array[indexPath.row] name];
    cell.detailTextLabel.text = [array[indexPath.row] number];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[_allProArray[section] zoneArray] count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CityTableViewController *city = [[CityTableViewController alloc] init];
    city.array = [[_allProArray[indexPath.section] zoneArray][indexPath.row] zoneArray];
    [self.navigationController pushViewController:city animated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _allProArray.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [_allProArray[section] name];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
